#include "Game.h"
#include <SDL.h>
#include <Windows.h>
#include <sstream>

using namespace std;

Game::Game() {
	// Initialize Game

	this->gameState = Playing;
	this->GraphicsPtr = new Graphics();

	Game::GameLoop();
}

void Game::GameLoop() {

	DWORD nextGameTick = GetTickCount();
	SDL_Event ev;

	while(true) {
		if(SDL_PollEvent(&ev)) {
			switch(ev.type) {
			case SDL_QUIT:
				{
					// End Game

					return;
				}
			case SDL_KEYDOWN:
				{
					// Handle Keyboard Input.
				}
			}
		}
		else {
			switch(this->gameState) {
			case Playing:
				{
					// Execute Game Logic until the set Ticks per Second are reached. Then Render
					while(GetTickCount() > nextGameTick) {
						static DWORD lastExecution;
						lastExecution = GetTickCount();

						nextGameTick += this->skipTicks;

						DWORD dTick = GetTickCount() - lastExecution;

						if(dTick > this->skipTicks) {
							ostringstream message;
							message << "Warning! Execution time took " << dTick << " ticks. Game is trying to catch up!" << endl;
							OutputDebugStringA(message.str().c_str());
						}
					}

					this->GraphicsPtr->RenderFrame();
				}
			case Paused:
				{
					// Pause Loop here
				}
			}
		}
	}
}